package web.admin.cms;

import domain.cms.AbstractPage;
import domain.cms.CmsPage;
import domain.cms.CmsUrl;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.facade.admin.CmsAdminServiceImpl;
import web.admin.components.TinyMceBehavior;

import java.util.List;


public final class CmsAttributeEdit extends Panel {
    @SpringBean
    CmsAdminServiceImpl cmsAdminService;

    public CmsAttributeEdit(String id, IModel<? extends AbstractPage> model) {
        super(id);
        final PropertyModel model1 = new PropertyModel(model, AbstractPage.TITLE);
        model1.getObject();
        add(new TextField("title", model1).setRequired(true));
        add(new TextField("linkName", new PropertyModel(model, AbstractPage.LINK_NAME)).setRequired(true));
        add(new TextField("description", new PropertyModel(model, AbstractPage.DESCRIPTION)));
        add(new TextField("keywords", new PropertyModel(model, AbstractPage.KEYWORDS)));

        TextArea textArea = new TextArea("content", new PropertyModel(model, AbstractPage.CONTENT));
        textArea.add(new TinyMceBehavior(true));
        add(textArea);

        WebMarkupContainer container = new WebMarkupContainer("urlContainer");
        container.add(new TextField("url", new PropertyModel(model, AbstractPage.URL)).setRequired(true));
        add(container);
        if (!(model.getObject() instanceof CmsPage)) {
            container.setVisible(false);
        }

        add(new CheckBox("published", new PropertyModel(model, AbstractPage.PUBLISHED)));
//       add(new CmsUrlListView("urls", model));
    }

    class CmsUrlListView extends ListView<CmsUrl> {
        public CmsUrlListView(String id, final IModel<? extends AbstractPage> model) {
            super(id);
            IModel<List<CmsUrl>> cmsUrlIModel = new LoadableDetachableModel<List<CmsUrl>>() {

                @Override
                protected List<CmsUrl> load() {
                    return model.getObject().getOldUrls();
                }
            };
            setModel(cmsUrlIModel);
        }

        @Override
        protected void populateItem(final ListItem<CmsUrl> item) {
            Button link = new Button("delete") {
                @Override
                public void onSubmit() {
                    super.onSubmit();
                }
            };
            item.add(link);
        }
    }
}
